package com.dyna.config;

import com.dyna.foundation.feign.FoundationDictApi;
import com.dyna.utils.DictLocalCacheUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author zhoucaiwang
 */
@Configuration
public class DynaDictAutoConfiguration {

    /**
     * PasswordEncoder 加密复杂度，越高开销越大
     */
    private final static Integer passwordEncoderLength = 4;

    @Bean(value = "dictUtils")
    @SuppressWarnings("InstantiationOfUtilityClass")
    public DictLocalCacheUtil dictUtils(@Qualifier("com.dyna.foundation.feign.FoundationDictApi") FoundationDictApi dictDataApi) {
        DictLocalCacheUtil.init(dictDataApi);
        return new DictLocalCacheUtil();
    }

    /**
     * Spring Security 加密器
     * 考虑到安全性，这里采用 BCryptPasswordEncoder 加密器
     *
     * @see <a href="http://stackabuse.com/password-encoding-with-spring-security/">Password Encoding with Spring Security</a>
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(passwordEncoderLength);
    }

}